using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class UILucky : MonoBehaviour
{
	private const int ANGLE_PER_BLOCK = 36;

	private const int ANGLE_PER_BLOCK_2 = 18;

	public GameObject turnPanel;

	public GameObject videoWnd;

	public Text txtTitle;

	public Text txtDesc;

	public MultiImageButton btnSpin;

	public Text txtSpinText;

	public AnimationTools spinAnimationTools;

	public AnimationTools arrowAnimationTools;

	private int totalDrawCount => CommonConfig.CommonCfgInfo.dayFreeDrawCount - DayRecord.instance.TodayGet(DayRecordKey.FREE_DRAW) + ConstantData.extraDrawCount;

	private void Start()
	{
		UILuckyItem[] componentsInChildren = base.gameObject.GetComponentsInChildren<UILuckyItem>();
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			UILuckyItem uILuckyItem = componentsInChildren[i];
			ProbInfo probInfo = LuckyConfig.config.probInfos[i];
			ItemInfo info = ItemInfoMgr.GetInfo(probInfo.itemId);
			Transform transform = uILuckyItem.transform;
			uILuckyItem.imgItemIcon = transform.Find("Image_Item").GetComponent<Image>();
			uILuckyItem.imgChipIcon = transform.Find("Image_Chip").GetComponent<Image>();
			uILuckyItem.imgChipIcon.gameObject.SetActive(info.isChip);
			uILuckyItem.imgItemIcon.gameObject.SetActive(!info.isChip);
			uILuckyItem.imgItemIcon.sprite = LaunchUIController.instance.GetSpriteFromName(info.icon);
			uILuckyItem.txtNumber = transform.Find("Number").GetComponent<Text>();
			if (info.type == 4 || info.type == 0)
			{
				uILuckyItem.txtNumber.text = probInfo.count.ToString();
			}
			else
			{
				uILuckyItem.txtNumber.text = info.name;
			}
			uILuckyItem.txtEmptyReward = transform.Find("NoReward").GetComponent<Text>();
		}
		RefreshText();
		if (EternalNode.Instance.lastDrawVideoTime > 0)
		{
			arrowAnimationTools.gameObject.SetActive(value: false);
		}
	}

	private void Update()
	{
	}

	private void OnClickGetItemOK()
	{
		int num = DayRecord.instance.TodayGet(DayRecordKey.FREE_DRAW);
		if (num == CommonConfig.CommonCfgInfo.dayFreeDrawCount)
		{
			if (AdManagerOld.instance.curAd.HasVideo())
			{
				if (EternalNode.Instance.IsTheSameDay())
				{
					arrowAnimationTools.gameObject.SetActive(value: true);
				}
			}
			else
			{
				EternalNode.Instance.lastDrawVideoTime = DateTime.Now.ToFileTime();
				RefreshText();
			}
		}
		if (!EternalNode.Instance.IsTheSameDay() && AdManagerOld.instance.curAd.HasVideo() && num == CommonConfig.CommonCfgInfo.dayFreeDrawCount && EternalNode.Instance.lastDrawVideoTime == 0)
		{
			videoWnd.SetActive(value: false);
			videoWnd.SetActive(value: true);
		}
	}

	public void RefreshText()
	{
		int num = DayRecord.instance.TodayGet(DayRecordKey.FREE_DRAW);
		if (num < totalDrawCount)
		{
			txtDesc.text = string.Format(HOLocalizationConfiguration.GetValue(431), totalDrawCount);
			txtTitle.gameObject.SetActive(value: false);
			btnSpin.interactable = true;
			spinAnimationTools.ActiveParam("Spin");
			return;
		}
		txtTitle.gameObject.SetActive(value: true);
		txtDesc.text = HOLocalizationConfiguration.GetValue(433);
		txtTitle.text = HOLocalizationConfiguration.GetValue(432);
		if (EternalNode.Instance.lastDrawVideoTime == 0)
		{
			btnSpin.interactable = true;
			txtTitle.gameObject.SetActive(value: false);
			spinAnimationTools.ActiveParam("Spin");
		}
		else
		{
			btnSpin.interactable = false;
			InvokeRepeating("CountDown", 1f, 0.03f);
			spinAnimationTools.FadeParam("Spin");
		}
	}

	public void ShowArrow()
	{
		if (EternalNode.Instance.lastDrawVideoTime == 0 && arrowAnimationTools.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("LuckyArrow-Out"))
		{
			arrowAnimationTools.Trigger("Show");
		}
	}

	private void CountDown()
	{
		TimeSpan drawDateTimeSpan = EternalNode.Instance.GetDrawDateTimeSpan();
		if (drawDateTimeSpan.TotalSeconds > 1.0)
		{
			txtTitle.text = HOLocalizationConfiguration.GetValue(432) + $"{drawDateTimeSpan.Minutes:D2}" + ":" + $"{drawDateTimeSpan.Seconds:D2}";
			txtSpinText.text = $"{drawDateTimeSpan.Minutes:D2}" + ":" + $"{drawDateTimeSpan.Seconds:D2}";
			return;
		}
		EternalNode.Instance.lastDrawVideoTime = 0L;
		btnSpin.interactable = true;
		txtTitle.gameObject.SetActive(value: false);
		txtSpinText.text = "旋转";
		spinAnimationTools.ActiveParam("Spin");
	}

	public void StartTurn()
	{
		arrowAnimationTools.gameObject.SetActive(value: false);
		int dayFreeDrawCount = CommonConfig.CommonCfgInfo.dayFreeDrawCount;
		int num = DayRecord.instance.TodayGet(DayRecordKey.FREE_DRAW);
		if (num >= dayFreeDrawCount && ConstantData.extraDrawCount <= 0)
		{
			// 旋转转盘
			if (!EternalNode.Instance.bLastClickClose)
			{
				/* if (AdManagerOld.instance.curAd.HasVideo())
				{ */
				videoWnd.SetActive(value: false);
				videoWnd.SetActive(value: true);
				/* }
				else
				{
					EternalNode.Instance.lastDrawVideoTime = DateTime.Now.ToFileTime();
					RefreshText();
				} */
			}
			else
			{
				WatchAD();
			}
			/* else if (AdManagerOld.instance.curAd.HasVideo())
			{
				WatchAD();
			}
			else
			{
				UIADViewWnd.ShowTip(HOLocalizationConfiguration.GetValue(179));
			}
			return; */
			return;
		}
		if (num < dayFreeDrawCount)
		{
			DayRecord.instance.TodayAdd(DayRecordKey.FREE_DRAW);
		}
		else if (ConstantData.extraDrawCount > 0)
		{
			ConstantData.extraDrawCount--;
		}
		ProbInfo probInfo = LuckyConfig.RandomLuckyInfo();
		if (probInfo == null)
		{
			UnityEngine.Debug.LogError("luckinfo is null!");
			return;
		}
		int id = probInfo.id;
		int num2 = UnityEngine.Random.Range(9, 12);
		int num3 = (id - 1) * 36 + num2 * -360 + 18;
		Hashtable hashtable = iTween.Hash("time", 3f, "easetype", iTween.EaseType.easeInOutCubic, "rotation", new Vector3(0f, 0f, num3));
		hashtable.Add("oncomplete", "OnTweenEnd");
		hashtable.Add("oncompleteparams", probInfo);
		hashtable.Add("oncompletetarget", base.gameObject);
		iTween.RotateTo(turnPanel, hashtable);
		btnSpin.interactable = false;
		LaunchUIController.instance.RefreshLuckyTip();
	}

	private void OnTweenEnd(ProbInfo luckyInfo)
	{
		UIADViewWnd.ShowGetItem(luckyInfo.itemId, luckyInfo.count, OnClickGetItemOK);
		ItemMgr.instance.AddItem(luckyInfo.itemId, luckyInfo.count);
		RefreshText();
	}

	private void WatchAD()
	{
		/* if (SDKTools.instance.CanPlayVedio())
		{ */
		SDKTools.instance.PlayVideoByDraw();
		//}
	}

	public void CloseVideoWnd()
	{
		EternalNode.Instance.bLastClickClose = true;
	}

	public void ClickWatchAD()
	{
		EternalNode.Instance.bLastClickClose = true;
		/* if (AdManagerOld.instance.curAd.HasVideo())
		{ */
		WatchAD();
		/* }
		else
		{
			UIADViewWnd.ShowTip(HOLocalizationConfiguration.GetValue(179));
		} */
	}

	public void ClickBack()
	{
		AdManagerOld.instance.curAd.HideBanner();
	}
}
